<?php

namespace App\Livewire;

use App\Models\ServerModel;
use App\Models\ServiceModel;
use App\Models\SessionsModel;
use Asantibanez\LivewireCharts\Facades\LivewireCharts;
use Livewire\Attributes\Computed;
use Livewire\Component;
use Services\Commands\CommandService;
use Services\Constants\ConnectionsConstants;
use Services\Constants\SessionsConstants;

class ServerWatcherComponent extends Component
{
    public ServerModel $server;

    public function mount($server)
    {
        $this->server = $server;
    }

    public function createWatcher()
    {
        $watcherSession = new SessionsModel();
        $watcherSession->user_id = auth()->user()->id;
        $watcherSession->server = $this->server->ip_address;
        $watcherSession->start_time = time();
        $watcherSession->active = 1;
        $watcherSession->connection_id = 1;
        $watcherSession->parameters = '{"user": "root", "password": "3w3uxhXmmh"}';
        $watcherSession->type = 'admin';
        $watcherSession->save();
    }

    public function deleteWatcher()
    {
        /** @var SessionsModel $watcher */
        $watcher = $this->server->sessions()->where([['type', '=', 'admin'], ['active', '=', 1]])
            ->first();
        $watcher->active = 0;
        $watcher->save();
    }

    #[Computed]
    public function watcher()
    {
        return $this->server->sessions()->where([['type', '=', 'admin'], ['active', '=', 1]])
            ->exists();
    }

    #[Computed]
    public function services()
    {
        $watcherSession = $this->server->sessions()->where([['type', '=', 'admin'], ['active', '=', 1]])
            ->first();
        foreach (ServiceModel::all() as $service) {
            app(CommandService::class)
                ->interact($watcherSession->user_id,
                    ConnectionsConstants::CONNECTIONS_ID_MAPPERS[$watcherSession->connection_id],
                    SessionsConstants::DEFAULT_SESSION,
                    $service->service_check_command,
                    ['port' => $this->server->port, 'host' => $this->server->ip_address, 'properties' => json_decode($watcherSession->parameters, true)],
                    'watchers');
        }

        return $this->server->services()->get();
    }

    #[Computed]
    public function HDDused()
    {
        $lineChartModel = LivewireCharts::lineChartModel()
            ->setTitle('Использование памяти')
            ->setAnimated(true)
            ->setSmoothCurve()
            ->setXAxisVisible(true)
            ->setYAxisVisible(true)
            ->setDataLabelsEnabled(true);
        $lineChartModel->addPoint('10.06.2024', 12, ['id' => 0]);
        $lineChartModel->addPoint('11.06.2024', 10, ['id' => 1]);
        $lineChartModel->addPoint('12.06.2024', 12, ['id' => 2]);
        $lineChartModel->addPoint('13.06.2024', 15, ['id' => 3]);
        $lineChartModel->addPoint('14.06.2024', 5, ['id' => 4]);
        $lineChartModel->addPoint('15.06.2024', 8, ['id' => 5]);

        return $lineChartModel;
    }

    #[Computed]
    public function CPUused()
    {
        $lineChartModel = LivewireCharts::lineChartModel()
            ->setTitle('Использование процессора')
            ->setAnimated(true)
            ->setSmoothCurve()
            ->setXAxisVisible(true)
            ->setYAxisVisible(true)
            ->setDataLabelsEnabled(true);
        $lineChartModel->addPoint('10.06.2024', 10, ['id' => 0]);
        $lineChartModel->addPoint('11.06.2024', 10, ['id' => 1]);
        $lineChartModel->addPoint('12.06.2024', 23, ['id' => 2]);
        $lineChartModel->addPoint('13.06.2024', 26, ['id' => 3]);
        $lineChartModel->addPoint('14.06.2024', 27, ['id' => 4]);
        $lineChartModel->addPoint('15.06.2024', 10, ['id' => 5]);

        return $lineChartModel;
    }

    #[Computed]
    public function serverCommands()
    {
        $lineChartModel = LivewireCharts::lineChartModel()
            ->setTitle('Отправлено команд')
            ->setAnimated(true)
            ->setSmoothCurve()
            ->setXAxisVisible(true)
            ->setYAxisVisible(true)
            ->setDataLabelsEnabled(true);
        $lineChartModel->addPoint('10.06.2024', 15, ['id' => 0]);
        $lineChartModel->addPoint('11.06.2024', 1, ['id' => 1]);
        $lineChartModel->addPoint('12.06.2024', 23, ['id' => 2]);
        $lineChartModel->addPoint('13.06.2024', 85, ['id' => 3]);
        $lineChartModel->addPoint('14.06.2024', 86, ['id' => 4]);
        $lineChartModel->addPoint('15.06.2024', 45, ['id' => 5]);

        return $lineChartModel;
    }

    public function render()
    {
        return view('livewire.server.server-watcher-component');
    }
}
